﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Data;
using System.Windows.Input;
using JulMar.Silverlight.Mvvm;
using MediaBrowserSL.RIA.MediaBrowserServiceReference;
using MediaBrowserSL.RIA.Services;
using System;

namespace MediaBrowserSL.RIA.ViewModels
{
    /// <summary>
    /// Page View Model Base
    /// </summary>
    public abstract class PageViewModelBase : ViewModelBase
    {
        #region Private Member
        private string mediaLookFor;
        #endregion

        #region Protected Properties
        protected ObservableCollection<Media> medias;
        protected CollectionViewSource sourceMediasView = new CollectionViewSource();
        protected ObservableCollection<Media> mediaItems;
        protected CollectionViewSource sourceMediaItemsView = new CollectionViewSource();
        protected object selectedItem;
        protected Media lastMediaSelected;
        #endregion

        #region Public Properties

        public abstract string Name { get; }

        public ICollectionView MediasView
        {
            get
            {
                return sourceMediasView.View;
            }
        }

        public ObservableCollection<Media> Medias
        {
            get
            {
                return medias;
            }
            set
            {
                medias = value;
            }
        }

        public ICollectionView MediaItemsView
        {
            get
            {
                return sourceMediaItemsView.View;
            }
        }

        public ObservableCollection<Media> MediaItems
        {
            get
            {
                return mediaItems;
            }
            set
            {
                mediaItems = value;
            }
        }

        public Media CurrentMedia
        {
            get
            {
                if (MediasView != null)
                {
                    return MediasView.CurrentItem as Media;
                }

                return null;

            }
        }

        public Media CurrentMediaItem
        {
            get
            {
                if (MediaItemsView != null)
                {
                    return MediaItemsView.CurrentItem as Media;
                }

                return null;
            }
        }

        public string MediaLookFor
        {
            set
            {
                mediaLookFor = value;

                if (!String.IsNullOrEmpty(value))
                {
                    if (SelectedItem != null) { SelectedItem = null; }
                }

                MediasView.Filter = item =>
                {
                    if (item as Media != null)
                    {
                        if ((item as Media).Name.ToUpper().Contains(value.ToUpper()))
                        {
                            return true;
                        }
                    }

                    return false;
                };

                OnPropertyChanged("MediaLookFor");
            }
            get
            {
                return mediaLookFor;
            }
        }

        public object SelectedItem
        {
            get
            {
                return this.selectedItem;
            }
            set
            {
                this.selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }

        #endregion

        #region Command

        public ICommand FilterCommand
        {
            get;
            private set;
        }

        #endregion Command

        protected IMediaRiaServices service;

        public PageViewModelBase(IMediaRiaServices service)
            : base()
        {
            this.service = service;
            FilterCommand = new DelegatingCommand<string>(Filter);
        }

        private void Filter(string val)
        {
            MediaLookFor = val;
        }
    }
}
